package com.vip.sys.modular.resources.controller;

import com.vip.aop.annotations.LocalLock;
import com.vip.common.annotations.CommonOperationLog;
import com.vip.common.enums.BusinessType;
import com.vip.common.response.CommonResponse;
import com.vip.sys.modular.resources.entity.Resources;
import com.vip.sys.modular.resources.params.AddResourcesParams;
import com.vip.sys.modular.resources.service.ResourcesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author tianlianyin
 * @date 2024/11/11
 */
@RestController
@Tag(name = "资源管理控制器")
@RequestMapping("/sys/resource")
public class ResourcesController {
    @Resource
    private ResourcesService resourceService;
    @GetMapping("/getResourcesTree")
    @Operation(summary = "获取资源树")
    public CommonResponse getResourcesTree() {
        return CommonResponse.success(resourceService.getResourcesTree());
    }
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:resource:add')")
    @LocalLock(key = "sys:resource:add:{#addResourcesParams.name}:{#addResourcesParams.parentId}")
    @CommonOperationLog(title = "添加菜单",businessType = BusinessType.INSERT)
    @Operation(summary = "添加资源")
    public CommonResponse add(@RequestBody AddResourcesParams addResourcesParams) {
        String message = resourceService.add(addResourcesParams) ? "添加成功" : "添加失败";
        return CommonResponse.success(message);
    }
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys:resource:edit')")
    @CommonOperationLog(title = "编辑菜单",businessType = BusinessType.UPDATE)
    @Operation(summary = "编辑资源")
    public CommonResponse edit(@RequestBody Resources resources) {
        if (resourceService.edit(resources))
            return CommonResponse.success("修改成功");
        return CommonResponse.success("");
    }
    @GetMapping("/getRoute")
    @Operation(summary = "获取路由")
    public CommonResponse getRoute() {
        return CommonResponse.success(resourceService.getRoute());
    }
    @GetMapping("/getGrantedResourcesTree")

    @Operation(summary = "获取授权资源树")
    public CommonResponse getGrantedResourcesTree() {
        return CommonResponse.success(resourceService.getGrantedResourcesTree());
    }
}
